<?php
/**
 * Created by PhpStorm.
 * User: skytop
 * Date: 16-8-28
 * Time: 下午12:48
 */

namespace Admin\Controller;

use Think\Controller;
use Admin\Model\Category;
class CategoryController extends Controller
{
    public function addCate(){
        if(IS_POST){
            $data=array(
                'name'=>I('post.name'),
                'sort'=>I('post.sort'),
                'pid'=>I('post.pid')
            );
            $cateTable=D('cate');
            if($cateTable->create($data)){
                $cateTable->add();
                $this->success('添加栏目成功!',U('Category/index'));
            }else{
                $this->error($cateTable->getError());
            }
        }else {
            $this->pid=I('get.pid',0,'intval');
            $cateTable=M('cate');
            if(isset($_GET['pid'])){
                $this->fat = $cateTable->where("id=" . I('get.pid'))->getField("name");
            }
            /*dump($this->pid);*/
            $this->display();
        }
    }

    public function index(){
        $cateTable=M("cate");
        $this->list=$list=$cateTable->order("sort ASC")->select();
        $cate=Category::unlimitedForLevel($list);
        /*print_r($cate);*/
        $this->cate=$cate;

        /*$ya=Category::unlimitedForlayer($list);
        print_r($ya);
        $nav=Category::getParents($list,8);
        print_r($nav);
        $fenlei=Category::getChildsId($list,4);
        print_r($fenlei);*/

        $this->display();
    }

    public function sortCate(){
        $cateTable=M('cate');
        foreach($_POST as $id=>$sort){
            if(is_numeric($sort)) {
                $cateTable->where(array('id' => $id))->setField('sort', $sort);
            }
        }
        $this->redirect('Category/index','',5,'修改成功,页面跳转中...');
    }
}